using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Hanoi.Ui
{
    /// <summary>
    /// Summary description for DisksConfigUi.
    /// </summary>
    public class DisksConfigUi
    {
        private Graphics g;
        private int _width;
        private int _height;
        private Bitmap _bitmap;
        private int _towerWidth;
        private int _margin = 1;
        private int _paddingHorizontal = 1;
        private int _blockHeight = 7;
        private int _disks = 1;

        public DisksConfigUi(int width, int height)
        {
            _width = width;
            _height = height;
            _towerWidth = _width;

            _bitmap = new Bitmap(width, height);
            g = Graphics.FromImage(_bitmap);
        }

        public Image GetImage()
        {
            return _bitmap;
        }

        public void Refresh(int disks)
        {
            g.Clear(SystemColors.Window);
            _disks = disks;
            PaintBlocks(disks);
        }

        private void PaintBlocks(int disks)
        {
            for (int y = disks; y > 0; y--)
                    PaintSingleBlock(0, y, disks - y + 1);
        }

        private void PaintSingleBlock(int tower, int position, int level)
        {
            PaintSingleBlock(tower, position, level, false);
        }

        private void PaintSingleBlock(int tower, int position, int level, bool inversedColors)
        {
            int textPadding = 2;
            int extra = (_disks - level) * 3;
            int left = _towerWidth * tower + _paddingHorizontal + extra;
            int width = _towerWidth - _paddingHorizontal * 2 - extra * 2;
            int top = _height - _blockHeight * (position + 1) - 2 * _margin * (position + 1);

            Color ForeColor = SystemColors.ActiveCaptionText;
            Color BackColor = SystemColors.ActiveCaption;
            if (inversedColors)
            {
                ForeColor = Color.FromArgb(192, 192, 192);
                BackColor = Color.Black;
            }

            Brush ForeBrush = new SolidBrush(ForeColor);
            Pen ForePen = new Pen(ForeColor);
            Brush BackBrush = new SolidBrush(BackColor);

            Rectangle rect = new Rectangle(left, top, width, _blockHeight);
            Font font = new Font("Nina", 4f, FontStyle.Bold);

            g.FillRectangle(BackBrush, rect);
            rect.Height -= 1;
            //g.DrawRectangle(ForePen, rect);
            g.DrawString(level.ToString(), font, ForeBrush, left + textPadding, top);
        }
    }
}
